package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.StatusInfo;
import com.lkd.contract.SupplyChannel;
import com.lkd.contract.SupplyContract;
import com.lkd.contract.VmStatusContract;
import com.lkd.emq.Topic;
import com.lkd.feign.VMService;
import com.lkd.http.vo.TaskDetailsViewModel;
import com.lkd.http.vo.TaskViewModel;
import com.lkd.service.TaskService;
import com.lkd.utils.JsonUtil;
import com.lkd.vo.VmVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Topic(TopicConfig.TASK_SUPPLY_TOPIC)
@Slf4j
public class CreateSupplyTaskMsgHandler implements MsgHandler {

    @Autowired
    private TaskService taskService;
    @Autowired
    private VMService vmService;

    @Override
    public void process(String jsonMsg) throws IOException {
        try{
            if(StringUtils.isEmpty(jsonMsg)){
                log.error(TopicConfig.TASK_SUPPLY_TOPIC + "接收到消息内容为空!");
                return;
            }
            //1.接收消息
            SupplyContract contract = JsonUtil.getByJson(jsonMsg, SupplyContract.class);

            //2.校验数据
            if(StringUtils.isEmpty(contract.getInnerCode()) ||
                    CollectionUtils.isEmpty(contract.getSupplyData())
            ){
                log.error(TopicConfig.TASK_SUPPLY_TOPIC+"消息数据异常" + jsonMsg );
                return;
            }

            String innerCode = contract.getInnerCode();
            List<SupplyChannel> supplyData = contract.getSupplyData();

            //远程调用售货机信息
            VmVO vmInfo = vmService.getVMInfo(innerCode);
            if(vmInfo == null){
                log.error("售货机上报状态信息，编码有误"+innerCode);
                return;
            }

            TaskViewModel model = new TaskViewModel();
            model.setAssignorId(0);
            model.setInnerCode(innerCode);
            model.setCreateType(VMSystem.TASK_CREATE_TYPE_AUTO);
            model.setProductType(VMSystem.TASK_TYPE_SUPPLY);

            //获取最少工单用户
            Integer userId = taskService.getLeastTaskCountUser(vmInfo.getRegionId(), true);
            model.setUserId(userId);
            //自动创建维修工单中json放进去了，这里json比较长，不再放
            //model.setDesc(jsonMsg);

            //通过消息传递过来货道信息，生成新的补货详情数据
            List<TaskDetailsViewModel> details = supplyData.stream().map(data -> {
                        TaskDetailsViewModel taskDetailsViewModel = new TaskDetailsViewModel();
                        taskDetailsViewModel.setChannelCode(data.getChannelId());
                        taskDetailsViewModel.setExpectCapacity(data.getCapacity());
                        taskDetailsViewModel.setSkuId(data.getSkuId());
                        taskDetailsViewModel.setSkuImage(data.getSkuImage());
                        taskDetailsViewModel.setSkuName(data.getSkuName());
                        return taskDetailsViewModel;
                    }
            ).collect(Collectors.toList());


            model.setDetails(details);

            taskService.create(model);
        }catch (Exception e){
            log.error("执行自动创建补货工单失败!",e);
        }

    }
}
